package com.jiangsong.rule;

import lombok.extern.slf4j.Slf4j;
import org.kie.api.runtime.KieContainer;
import org.kie.api.runtime.KieSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Slf4j
@Service
public class RuleService {
    @Autowired
    private KieContainer kieContainer;

    public FeeCalculateEntity getDiscount(FeeCalculateEntity feeCalculateEntity) {
        System.out.println("开始service");

        // 开启会话
        KieSession kieSession = kieContainer.newKieSession();
        // 设置折扣对象
        // 设置订单对象
        kieSession.insert(feeCalculateEntity);
        // 触发规则
        kieSession.fireAllRules();
        //或者  通过规则过滤器实现只执行指定规则
        //kieSession.fireAllRules(new RuleNameEqualsAgendaFilter("Age based discount"));
        // 中止会话
        kieSession.dispose();
        System.out.println("结束service");
        return feeCalculateEntity;
    }
}
